﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using HtmlDocument = HtmlAgilityPack.HtmlDocument;

namespace 第七次作业
{
    internal class SearchHandler
    {
        private WebClient client;

        public SearchHandler() {
            client = new WebClient
            {
                Encoding = Encoding.UTF8
            };
        }

        public async Task<string> GetBaikeSummaryAsync(string url)
        {
            string html = await client.DownloadStringTaskAsync(url);
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);

            HtmlNode summaryNode = doc.DocumentNode.SelectSingleNode("//div[@class='lemma-summary J-summary']");
            if (summaryNode != null)
            {
                return summaryNode.InnerText.Trim();
            }
            else
            {
                return "搜索失败";
            }
        }
    }
}
